﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile
{
	public class ParserException : Exception
	{

		// TODO: this needs to be removed and the token needs to be determined.
		public ParserException(string message)
			: base(message)
		{ }

		public ParserException(string message, string file, string line, int lineNum, int col)
			: base(message + "\nLine: " + (lineNum + 1) + ", Column: " + (col + 1) + " in " + file)
		{ }

		public ParserException(string message, Token offendingToken)
			: this(message, offendingToken.File, offendingToken.Line, offendingToken.LineNum, offendingToken.Column)
		{ }

		public static ParserException UnexpectedToken(Tokens tokens, Token token)
		{
			if (token == null)
			{
				return UnexpectedEOF(tokens);
			}
			return new ParserException("Unexpected token: \"" + token.Value + "\"", token);
		}

		public static ParserException UnexpectedEOF(Tokens tokens)
		{
			string file = tokens.FileName;
			string[] lines = tokens.FileOriginalContents.Split('\n');
			int lineNum = lines.Length;
			string line = lines[lineNum - 1];
			int col = line.Length;
			return new ParserException("Unexpected EOF", file, line, lineNum, col);
		}
	}
}
